{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# 父类A\r\n",
    "class A(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.num = 1\r\n",
    "\r\n",
    "    def info_print(self):\r\n",
    "        print(self.num)\r\n",
    "\r\n",
    "# 子类B\r\n",
    "class B(A):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "result = B()\r\n",
    "result.info_print()  # 1"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "1\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 单继承"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "# 1. 师父类\r\n",
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "        \r\n",
    "# 2. 徒弟类\r\n",
    "class Prentice(Master):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "# 3. 创建对象daqiu\r\n",
    "daqiu = Prentice()\r\n",
    "# 4. 对象访问实例属性\r\n",
    "print(daqiu.kongfu)\r\n",
    "# 5. 对象调用实例方法\r\n",
    "daqiu.make_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[古法煎饼果子配方]\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多继承"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "# 创建学校类\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "print(daqiu.kongfu)\r\n",
    "daqiu.make_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[黑马煎饼果子配方]\n",
      "运用[黑马煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 重写父类同名属性和方法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "# 独创配方\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "print(daqiu.kongfu)\r\n",
    "daqiu.make_cake()\r\n",
    "\r\n",
    "print(Prentice.__mro__)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[独创煎饼果子配方]\n",
      "运用[独创煎饼果子配方]制作煎饼果子\n",
      "(<class '__main__.Prentice'>, <class '__main__.School'>, <class '__main__.Master'>, <class 'object'>)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 调用父类同名属性和方法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        # 如果是先调用了父类的属性和方法，父类属性会覆盖子类属性，故在调用属性前，先调用自己子类的初始化\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    # 调用父类方法，但是为保证调用到的也是父类的属性，必须在调用方法前调用父类的初始化\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "daqiu.make_cake()\r\n",
    "\r\n",
    "daqiu.make_master_cake()\r\n",
    "\r\n",
    "daqiu.make_school_cake()\r\n",
    "\r\n",
    "daqiu.make_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运用[独创煎饼果子配方]制作煎饼果子\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n",
      "运用[黑马煎饼果子配方]制作煎饼果子\n",
      "运用[独创煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        # 如果是先调用了父类的属性和方法，父类属性会覆盖子类属性，故在调用属性前，先调用自己子类的初始化\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    # 调用父类方法，但是为保证调用到的也是父类的属性，必须在调用方法前调用父类的初始化\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "daqiu.make_cake()\r\n",
    "daqiu.make_cake()\r\n",
    "\r\n",
    "daqiu.make_master_cake()\r\n",
    "\r\n",
    "daqiu.make_school_cake()\r\n",
    "\r\n",
    "daqiu.make_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运用[独创煎饼果子配方]制作煎饼果子\n",
      "运用[独创煎饼果子配方]制作煎饼果子\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n",
      "运用[黑马煎饼果子配方]制作煎饼果子\n",
      "运用[黑马煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多层继承"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "\r\n",
    "# 徒孙类\r\n",
    "class Tusun(Prentice):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "xiaoqiu = Tusun()\r\n",
    "\r\n",
    "xiaoqiu.make_cake()\r\n",
    "\r\n",
    "xiaoqiu.make_school_cake()\r\n",
    "\r\n",
    "xiaoqiu.make_master_cake()\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运用[独创煎饼果子配方]制作煎饼果子\n",
      "运用[黑马煎饼果子配方]制作煎饼果子\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## super() 调用父类方法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "        # 方法2.1\r\n",
    "        # super(School, self).__init__()\r\n",
    "        # super(School, self).make_cake()\r\n",
    "\r\n",
    "        # 方法2.2\r\n",
    "        super().__init__()\r\n",
    "        super().make_cake()\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子技术]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    # 子类调用父类的同名方法和属性：把父类的同名属性和方法再次封装\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "    # 一次性调用父类的同名属性和方法\r\n",
    "    def make_old_cake(self):\r\n",
    "        # 方法一：代码冗余；父类类名如果变化，这里代码需要频繁修改\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "        # 方法二: super()\r\n",
    "        # 方法2.1 super(当前类名, self).函数()\r\n",
    "        # super(Prentice, self).__init__()\r\n",
    "        # super(Prentice, self).make_cake()\r\n",
    "\r\n",
    "        # 方法2.2 super().函数()\r\n",
    "        # super().__init__()\r\n",
    "        # super().make_cake()\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "daqiu.make_old_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运用[黑马煎饼果子配方]制作煎饼果子\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "class Prentice(School):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子技术]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    # 子类调用父类的同名方法和属性：把父类的同名属性和方法再次封装\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "    # 一次性调用父类的同名属性和方法\r\n",
    "    def make_old_cake(self):\r\n",
    "        # 方法一：代码冗余；父类类名如果变化，这里代码需要频繁修改\r\n",
    "        # Master.__init__(self)\r\n",
    "        # Master.make_cake(self)\r\n",
    "        # School.__init__(self)\r\n",
    "        # School.make_cake(self)\r\n",
    "\r\n",
    "        # 方法二: super()\r\n",
    "        # 方法2.1 super(当前类名, self).函数()\r\n",
    "        super(Prentice, self).__init__()\r\n",
    "        super(Prentice, self).make_cake()\r\n",
    "\r\n",
    "        # 方法2.2 super().函数()\r\n",
    "        # super().__init__()\r\n",
    "        # super().make_cake()\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "daqiu.make_old_cake()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运用[黑马煎饼果子配方]制作煎饼果子\n",
      "运用[古法煎饼果子配方]制作煎饼果子\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "class Prentice(School):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子技术]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    # 子类调用父类的同名方法和属性：把父类的同名属性和方法再次封装\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "    # 一次性调用父类的同名属性和方法\r\n",
    "    def make_old_cake(self):\r\n",
    "        # 方法一：代码冗余；父类类名如果变化，这里代码需要频繁修改\r\n",
    "        # Master.__init__(self)\r\n",
    "        # Master.make_cake(self)\r\n",
    "        # School.__init__(self)\r\n",
    "        # School.make_cake(self)\r\n",
    "\r\n",
    "        # 方法二: super()\r\n",
    "        # 方法2.1 super(当前类名, self).函数()\r\n",
    "        # super(Prentice, self).__init__()\r\n",
    "        # super(Prentice, self).make_cake()\r\n",
    "\r\n",
    "        # 方法2.2 super().函数()\r\n",
    "        super().__init__()\r\n",
    "        super().make_cake()\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "daqiu.make_old_cake()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 私有权限"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "        # 定义私有属性\r\n",
    "        self.__money = 2000000\r\n",
    "\r\n",
    "    # 定义私有方法\r\n",
    "    def __info_print(self):\r\n",
    "        print(self.kongfu)\r\n",
    "        print(self.__money)\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "\r\n",
    "# 徒孙类\r\n",
    "class Tusun(Prentice):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "# 对象不能访问私有属性和私有方法\r\n",
    "print(daqiu.__money)\r\n",
    "daqiu.__info_print()"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'Prentice' object has no attribute '__money'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-6d50f80a2f85>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     46\u001b[0m \u001b[0mdaqiu\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPrentice\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     47\u001b[0m \u001b[1;31m# 对象不能访问私有属性和私有方法\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 48\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdaqiu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__money\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     49\u001b[0m \u001b[0mdaqiu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__info_print\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Prentice' object has no attribute '__money'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "xiaoqiu = Tusun()\r\n",
    "# 子类无法继承父类的私有属性和私有方法\r\n",
    "print(xiaoqiu.__money)  # 无法访问实例属性__money\r\n",
    "xiaoqiu.__info_print()"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'Tusun' object has no attribute '__money'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-c432d878d4cb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mxiaoqiu\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTusun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 子类无法继承父类的私有属性和私有方法\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mxiaoqiu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__money\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 无法访问实例属性__money\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mxiaoqiu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__info_print\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Tusun' object has no attribute '__money'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 获取和修改私有属性值"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "class Master(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[古法煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class School(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[黑马煎饼果子配方]'\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "\r\n",
    "class Prentice(School, Master):\r\n",
    "    def __init__(self):\r\n",
    "        self.kongfu = '[独创煎饼果子配方]'\r\n",
    "        self.__money = 2000000\r\n",
    "\r\n",
    "    # 获取私有属性\r\n",
    "    def get_money(self):\r\n",
    "        return self.__money\r\n",
    "\r\n",
    "    # 修改私有属性\r\n",
    "    def set_money(self):\r\n",
    "        self.__money = 500\r\n",
    "\r\n",
    "    def __info_print(self):\r\n",
    "        print(self.kongfu)\r\n",
    "        print(self.__money)\r\n",
    "\r\n",
    "    def make_cake(self):\r\n",
    "        self.__init__()\r\n",
    "        print(f'运用{self.kongfu}制作煎饼果子')\r\n",
    "\r\n",
    "    def make_master_cake(self):\r\n",
    "        Master.__init__(self)\r\n",
    "        Master.make_cake(self)\r\n",
    "\r\n",
    "    def make_school_cake(self):\r\n",
    "        School.__init__(self)\r\n",
    "        School.make_cake(self)\r\n",
    "\r\n",
    "\r\n",
    "# 徒孙类\r\n",
    "class Tusun(Prentice):\r\n",
    "    pass\r\n",
    "\r\n",
    "\r\n",
    "daqiu = Prentice()\r\n",
    "\r\n",
    "xiaoqiu = Tusun()\r\n",
    "# 调用get_money函数获取私有属性money的值\r\n",
    "print(xiaoqiu.get_money())\r\n",
    "# 调用set_money函数修改私有属性money的值\r\n",
    "xiaoqiu.set_money()\r\n",
    "print(xiaoqiu.get_money())"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "2000000\n",
      "500\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "调用父类的"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.6.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}